/**
 * 
 */
package com.et114.modules.products.cellphone.services;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import com.et114.components.simpletable.DataList;
import com.et114.core.services.impl.BaseServicesImpl;
import com.et114.modules.products.base.ProImage;
import com.et114.modules.products.cellphone.dao.CellPhoneDao;
import com.et114.modules.products.cellphone.vo.CellPhone;
import com.et114.modules.products.cellphone.vo.CellPhoneDomain;
import com.et114.modules.products.commons.Const;
import com.et114.modules.products.commons.Context;
import com.et114.modules.products.proservices.ProService;
import com.et114.modules.products.search.SearchVo;
import com.et114.modules.products.services.ProPropertySetImp;
import com.et114.modules.products.search.SearchService; 

/*
 * @author guanhw
 */
public class CellPhoneServiceImpl extends BaseServicesImpl {
	private CellPhoneDao	cellPhoneDao;
	private SearchService   searchCompass ; 

	public void setCellPhoneDao ( CellPhoneDao cellPhoneDao ) {
		this.cellPhoneDao = cellPhoneDao;
	}

	public void setSearchCompass ( SearchService searchCompass ) {
		this.searchCompass = searchCompass;
	}

	public DataList getCellPhones( CellPhone vo ) throws Exception { 
		return cellPhoneDao.getCellPhones ( vo  ) ; 
	}
	
	
	public List< CellPhone > getCellPhones4( CellPhone vo ) throws Exception {
		return cellPhoneDao.getCellPhones4 ( vo ) ; 
	}
	
	public List< CellPhone > getCellPhones4Manager( CellPhone vo ) throws Exception {
		return cellPhoneDao.getCellPhones4Manager ( vo ) ; 
	}
	
	public Long countgetCellPhones( CellPhone vo ) throws Exception {
		return cellPhoneDao.countgetCellPhones ( vo ) ; 
	}
	
	public Long countgetCellPhones4View( CellPhone vo ) throws Exception {
		return cellPhoneDao.countgetCellPhones4View ( vo ) ; 
	}	

	
	public List< CellPhone > getCellPhoneList( CellPhone vo ) throws Exception {
		return cellPhoneDao.getCellPhoneList ( vo ) ;  
	}
	
	public List< CellPhone > getCellPhonesHasOffTime( CellPhone vo ) throws Exception {
		return cellPhoneDao.getCellPhonesHasOffTime ( vo ) ; 
	}
	
	public CellPhone getCellPhoneById( Long id ) throws Exception {
		return cellPhoneDao.getCellPhoneById ( id ) ;
	}
	
	
	public Long saveCellPhone ( CellPhone vo ) throws Exception {
		Long id = cellPhoneDao.saveCellPhone ( vo );
		vo.setTjOrder ( id );
		updateCellPhone ( vo );
		return id;
	}

	public void updateCellPhone( CellPhone vo ) throws Exception {
		cellPhoneDao.updateCellPhone ( vo ) ; 
		CellPhone cp = cellPhoneDao.getCellPhoneById ( vo.getProId ( ) ) ; 
		if ( cp != null ) {
			if ( cp.getProOnOffStatus ( ) == Const.PRO_PUB_STATUS_PUBED ) {
				SearchVo svo = getCellPhone4Compass ( cp.getProId ( ) ) ; 
				if ( svo != null ) 
					searchCompass.setPro  ( cp.getProId ( ) ,  Const.PRO_TYPE_CELLPHONE  , svo ) ; 
			}
			else 
				searchCompass.removePro  ( cp.getProId ( ) , Const.PRO_TYPE_CELLPHONE ) ; 	
		}
	}	
	
	public void updateCellPhoneProOnOffStatus( CellPhone cp ) throws Exception {
		cellPhoneDao.updateCellPhoneProOnOffStatus ( cp ) ; 
		//
		if ( cp != null ) {
			if ( cp.getProOnOffStatus ( ) == Const.PRO_PUB_STATUS_PUBED ) {
				SearchVo svo = getCellPhone4Compass ( cp.getProId ( ) ) ; 
				if ( svo != null ) 
					searchCompass.setPro  ( cp.getProId ( ) ,  Const.PRO_TYPE_CELLPHONE  , svo ) ; 
			}
			else 
				searchCompass.removePro  ( cp.getProId ( ) , Const.PRO_TYPE_CELLPHONE ) ; 	
		}
	}
	
	public void removeCellPhone( Long id ) throws Exception {
		//delete compass
		SearchVo svo = getCellPhone4Compass ( id ) ; 
		if ( svo != null )
			searchCompass.removePro  ( id , Const.PRO_TYPE_CELLPHONE ) ; 

		//
		cellPhoneDao.removeCellPhone ( id ) ; 		
		Context.removeProRel ( id , Const.PRO_TYPE_CELLPHONE )		 ;
		
	}
	
	//
	public DataList getCellPhones4View ( CellPhone vo ) throws Exception {
		return cellPhoneDao.getCellPhones4View ( vo );
	}
	
	public CellPhone getBaseCellPhoneInfoById4View ( Long id ) throws Exception {
		return getBaseCellPhoneInfoById4View ( id , Const.PRO_PUB_STATUS_PUBED );
	}
	
	
	public CellPhone getBaseCellPhoneInfoById4View ( Long id ,
			Byte proOnOffStatus ) throws Exception {
		CellPhone cp = new CellPhone ( );
		cp.setProId ( id );
		cp.setProOnOffStatus ( proOnOffStatus );
		return cellPhoneDao.getBaseCellPhoneInfoById4View ( cp );
	}
	
	public List < CellPhone > getCellPhoneTopTen(  ) throws Exception {
		return cellPhoneDao.getCellPhoneTopTen( ) ;
	}
	
	public CellPhoneDomain getCellPhoneDomain( String params , ProPropertySetImp proPropertySetImp  )throws Exception {
		CellPhoneDomain domain = new CellPhoneDomain( ) ; 
		//Map< String , String > paramsMap = new HashMap< String , String > () ; 
		
		if ( !StringUtils.isEmpty ( params ) && !StringUtils.isBlank ( params )  ) {
			
		}
	
		return domain ; 
	}
	
	//interface
	public CellPhone getOrderCellPhone( Long proId , Byte color , Long proServiceId , String COUNTRY  ) throws Exception {
		Assert.notNull ( proId  ) ; 
		Assert.notNull ( color  ) ; 
		Assert.notNull ( proServiceId  ) ; 
		Assert.notNull ( COUNTRY  ) ; 
		CellPhone cellPhone = getBaseCellPhoneInfoById4View( proId , null ) ; 
		if ( cellPhone == null ) return null;
		/*
		if ( cellPhone.getProOnOffStatus ( ) != Const.PRO_PUB_STATUS_PUBED )
			return null; */
		List < ProImage > proImages = Context
				.getProImageServiceImpl ( )
				.getProImageList ( 
						new ProImage ( proId , Const.PRO_TYPE_CELLPHONE , color ) );

		cellPhone.setProImages ( proImages ) ;
		
		ProService orderProService = Context.getProServiceServiceImpl ( ).getProServiceRelById( proServiceId , COUNTRY ) ;
		if ( orderProService == null ) return null ; 
		cellPhone.setOrderProService ( orderProService ) ;
		return cellPhone ; 
	}
	
	
	public SearchVo getCellPhone4Compass( Long proId ) {
		return cellPhoneDao.getCellPhone4Compass ( proId ) ;
	}	
}
